package com.hifar.sso.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.filter.CharacterEncodingFilter;

/**
 * 强制字符编码配置类
 * 确保SSO模块的字符编码强制使用UTF-8
 * 
 * @author system
 * @since 1.0.0
 */
@Configuration
public class ForceEncodingConfig {

    /**
     * 强制字符编码过滤器
     * 使用最高优先级确保编码设置生效
     */
    @Bean
    public FilterRegistrationBean<CharacterEncodingFilter> forceCharacterEncodingFilter() {
        FilterRegistrationBean<CharacterEncodingFilter> registration = new FilterRegistrationBean<>();
        
        CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter();
        characterEncodingFilter.setEncoding("UTF-8");
        characterEncodingFilter.setForceEncoding(true);
        characterEncodingFilter.setForceRequestEncoding(true);
        characterEncodingFilter.setForceResponseEncoding(true);
        
        registration.setFilter(characterEncodingFilter);
        registration.addUrlPatterns("/*");
        registration.setName("forceCharacterEncodingFilter");
        registration.setOrder(Ordered.HIGHEST_PRECEDENCE);
        
        return registration;
    }
}
